package com.pioneer.swing.controller;

import com.pioneer.swing.model.AllJobsTable;
import com.pioneer.dao.JobDAO;
import javax.swing.JTable;
import javax.swing.JInternalFrame;
import javax.swing.table.TableColumnModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/*
 * Author: gchen
 * Created: Friday, February 13, 2004 10:00:47 PM
 * Modified: Friday, February 13, 2004 10:00:47 PM
 */


public class TableSorter
extends MouseAdapter{
	
	private JTable tableView;
	
	private JobDAO jobDAO;
	
	public TableSorter(JTable table) {
		this.tableView = table;		
	}
	
	public void setJobsDAO(JobDAO jobDAO) {
		this.jobDAO = jobDAO;
	}
	
	public void mouseClicked(MouseEvent e) {		
        TableColumnModel columnModel = tableView.getColumnModel();
        int viewColumn = columnModel.getColumnIndexAtX(e.getX());
		if (tableView.getModel() instanceof AllJobsTable) {
			AllJobsTable model = (AllJobsTable)tableView.getModel();
			switch(viewColumn) {
			case AllJobsTable.JOB_CONTRACTOR:
				model.setSortField("contractor");
				break;
			case AllJobsTable.JOB_DATE:
				model.setSortField("job_date");
				break;
			case AllJobsTable.JOB_NAME:
				model.setSortField("job_name");				
				break;
			case AllJobsTable.JOB_QUOTE_NUM:
				model.setSortField("quote_num");
				break;
			case AllJobsTable.JOB_NOTES:
				model.setSortField("notes");
				break;
			case AllJobsTable.JOB_SALES_REP:
				model.setSortField("sales_rep");
				break;
			case AllJobsTable.JOB_STATE	:
				model.setSortField("state");
				break;
			case AllJobsTable.JOB_STATUS:
				model.setSortField("status");
				break;
			case AllJobsTable.JOB_WHOLESALER:
				model.setSortField("supplier_name");
				break;
			}
			model.refresh();
			tableView.revalidate();
			tableView.repaint();
		}
        //int column = tableView.convertColumnIndexToModel(viewColumn);
		/*
        if (e.getClickCount() == 1 && column != -1) {
            //System.out.println("Sorting ...");
            int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK;
            boolean ascending = (shiftPressed == 0);
            sorter.sortByColumn(column, ascending);
        }*/
    

	}
}
